package com.zymail.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zymail.bean.ZyClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.api.CommonResult;
import com.zymail.api.ResultCode;
import com.zymail.bean.ZyGood;
import com.zymail.bean.ZyGoodDe;
import com.zymail.bean.ZyGoodImg;
import com.zymail.constant.Constant;
import com.zymail.service.*;
import com.zymail.vo.GoodInfo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/good")
@Slf4j
public class GoodController extends BaseController {

    @Autowired
    public ZyGoodService    goodService;
    @Autowired
    public ZyClassService   zyClassService;
    @Autowired
    public ZyGoodDeService  zyGoodDeService;
    @Autowired
    public ZyGoodImgService zyGoodImgService;

    @Autowired
    public SaveGoodInfo     saveGoodInfo;

    /**
     * 返回分类列表的所有list
     *
     * @return
     */
    @GetMapping("/getall")
    public List<ZyGood> getSortedList() { return goodService.getSortedGood(); }

    /**
     * 返回分类列表的所有list
     *
     * @return
     */
    @RequestMapping("/all")
    @ResponseBody
    public CommonResult<List<GoodInfo>> all() {
        log.info("get all goods infomations");
        QueryWrapper<ZyGood> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("id");
        List<ZyGood> zyGoods = goodService.list(wrapper);
        List<GoodInfo> goodInfos = new ArrayList<>();
        System.out.println(zyGoods);
        for (ZyGood zygood : zyGoods) {
            if (zygood.getGoodDelist().size() > 0)
            for (ZyGoodDe zyGoodDe : zygood.getGoodDelist()) {
//                Integer classId = zygood.getClassId();
//                QueryWrapper<ZyClass> wrapper1 = new QueryWrapper<>();
//                wrapper1.eq("id", classId);
//                ZyClass entity = wrapper1.getEntity();
                GoodInfo goodInfo = GoodInfo.builder()
                        .category(zygood.getGoodclass().getName())
                        .name(zyGoodDe.getName())
                        .id(String.valueOf(zygood.getId()))
                        .details(zygood.getDe())
                        .image(zygood.getGoodImgs().get(0).getUrl())
                        .price(String.valueOf(zyGoodDe.getPrice()))
                        .stock(zyGoodDe.getPrice().toString())
                        .speci(zyGoodDe.getDe())
                        .build();
                goodInfos.add(goodInfo);
            }
            log.info(zygood.toString());
        }
        return new CommonResult<>(ResultCode.SUCCESS, goodInfos, Constant.ADD_GOODS_SUCCESS);
    }

    @RequestMapping("/save")
    public String save(ZyGood zyGood, Double price, Integer count, Integer deid, String url, String dename, Integer imgid) {

        goodService.saveOrUpdate(zyGood);
        QueryWrapper<ZyGood> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("id").last("limit 1");
        ZyGood good = goodService.getOne(wrapper);

        ZyGoodImg zyGoodImg = new ZyGoodImg();
        zyGoodImg.setUrl(url);
        if (imgid != null) {
            zyGoodImg.setId(imgid);
        }
        zyGoodImg.setGoodId(good.getId());
        zyGoodImgService.saveOrUpdate(zyGoodImg);

        ZyGoodDe zyGoodDe = new ZyGoodDe();
        zyGoodDe.setCount(count);
        if (deid != null) {
            zyGoodDe.setId(deid);
        }
        zyGoodDe.setPrice(price);
        zyGoodDe.setName(dename);
        zyGoodDe.setGoodId(good.getId());
        zyGoodDeService.saveOrUpdate(zyGoodDe);

        return "succ";
    }

    @PostMapping("/add")
    @ResponseBody
    public CommonResult<Integer> addGoods(GoodInfo goodInfo) {
        log.info("save goodInfo " + goodInfo);

        saveGoodInfo.saveGoodAllInfo(goodInfo);
        return new CommonResult<>(ResultCode.SUCCESS, null, Constant.ADD_GOODS_SUCCESS);
    }

    @RequestMapping("/del")
    public String del(ZyGood zyGood) {
        goodService.removeById(zyGood.getId());
        return "succ";
    }

    /**
     * 返回一个商品的信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getone/{id}")
    public ZyGood getOne(@PathVariable("id") Integer id) {
        return goodService.getById(id);
    }

}
